package utils;

import gui.MainPanel;
import gui.toolpanels.CNode;
import gui.toolpanels.JobsPanel;
import guiobj.SubmitPanelInfo;

import java.awt.Dimension;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.Insets;
import java.awt.Toolkit;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;

import javax.swing.JFrame;
import javax.swing.tree.DefaultMutableTreeNode;

import jsonobj.JobInfo;

import data.ClusterInfo;
import data.Files;
import data.JobData;
import data.UserInfo;

public class Utils {
	// TODO
	// error reporting
	// default results dir
	// cancel jobs
	// file handling from tree
	// form validation
	// security
	// user accounts, quotas, top panel
	public static data.WebSocket ws = null;
	public static MainPanel p;
	public static UserInfo ui;
	public static DecimalFormat df = new DecimalFormat("##.00");
	public static final String sire = "sire.dyndns.org";
	public static final String sire_host = "ws://sire.dyndns.org/wstest/";
	public static final String laptop_local_host = "ws://laptop.local:8080/wstest/";
	public static final String localhost = "ws://localhost:8080/wstest/";
	public static final String server = sire_host;
	// public static final String server = laptop_local_host;
	public static final String ssh_server = "laptop.local";
	// public static final String ssh_server = "sire.dyndns.org";
	public static String username;
	public static String passwd;
	public static long jobcount = 0;
	public static JobInfo ji = new JobInfo();
	public static Map<String, SubmitPanelInfo> jobs = new HashMap<String, SubmitPanelInfo>();
	public static Files files = new Files();
	public static CNode root;
	public static ExecutorService exec = Executors.newCachedThreadPool();

	public static void out(Object o) {
		System.out.println(o.toString());
	}

	public static void centrePositionOnScreen(JFrame frm) {
		Toolkit kit = frm.getToolkit();
		GraphicsEnvironment ge = GraphicsEnvironment
				.getLocalGraphicsEnvironment();
		GraphicsDevice[] gs = ge.getScreenDevices();
		Insets in = kit.getScreenInsets(gs[0].getDefaultConfiguration());

		Dimension d = kit.getScreenSize();
		int max_width = (d.width - in.left - in.right);
		int max_height = (d.height - in.top - in.bottom);
		// frm.setSize(Math.min(max_width, 850), Math.min(max_height,
		// 550));//whatever size you want but smaller the insets
		frm.setLocation((int) (max_width - frm.getWidth()) / 2,
				(int) (max_height - frm.getHeight()) / 2);
	}

	public static String zip(String path) {
		final int BUFFER = 2048;
		 try {
	         BufferedInputStream origin = null;
	         FileOutputStream dest = new 
	           FileOutputStream(path+".zip");
	         ZipOutputStream out = new ZipOutputStream(new 
	           BufferedOutputStream(dest));
	         //out.setMethod(ZipOutputStream.DEFLATED);
	         byte data[] = new byte[BUFFER];
	         // get a list of files from current directory
	         File f = new File(path);
	         String files[] = f.list();

	         for (int i=0; i<files.length; i++) {
	            System.out.println("Adding: "+files[i]);
	            FileInputStream fi = new 
	              FileInputStream(path+"\\"+files[i]);
	            origin = new 
	              BufferedInputStream(fi, BUFFER);
	            ZipEntry entry = new ZipEntry(files[i]);
	            out.putNextEntry(entry);
	            int count;
	            while((count = origin.read(data, 0, 
	              BUFFER)) != -1) {
	               out.write(data, 0, count);
	            }
	            origin.close();
	         }
	         out.close();
	         return path+".zip";
	      } catch(Exception e) {
	         e.printStackTrace();
	         return null;
	      }
	}

	public static void unzip(String path) {
		final int BUFFER = 2048;

		try {
			String dir = path.replace(".zip", "");
			if(!new File(dir).exists())
				new File(dir).mkdir();
			BufferedOutputStream dest = null;
			FileInputStream fis = new FileInputStream(path);
			ZipInputStream zis = new ZipInputStream(
					new BufferedInputStream(fis));
			ZipEntry entry;
			while ((entry = zis.getNextEntry()) != null) {
				System.out.println("Extracting: " + entry);
				int count;
				byte data[] = new byte[BUFFER];
				// write the files to the disk
				FileOutputStream fos = new FileOutputStream(dir+"\\"+entry.getName());
				dest = new BufferedOutputStream(fos, BUFFER);
				while ((count = zis.read(data, 0, BUFFER)) != -1) {
					dest.write(data, 0, count);
				}
				dest.flush();
				dest.close();
			}
			zis.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
